<template>
  <div class="common-layout">
    <el-container style="height: 100%; height: 100%">
      <el-header class="header-wrapped">
        <div class="header-content">
          <h3 style="color: #ffffff">小易后台管理系统</h3>
          <span class="welcome">欢迎您的登录</span>
        </div>
      </el-header>
      <el-main>
        <div class="login-warpper">
          <el-card class="box-card">
            <el-tabs v-model="activeName" class="demo-tabs" :stretch="true">
              <el-tab-pane label="登录" name="first">
                <el-form ref="el-form">
                  <el-form-item label="账号" prop="pass">
                    <el-input v-model="loginData.account" />
                  </el-form-item>
                  <el-form-item
                    label="密码"
                    prop="checkPass"
                    style="margin-top: 40px"
                  >
                    <el-input v-model="loginData.password" type="password" />
                  </el-form-item>

                  <el-form-item>
                    <div
                      style="width: 100%; text-align: right; margin-bottom: 5px"
                    >
                      <a href="#" @click.prevent="forgetPassword">忘记密码</a>
                    </div>
                    <el-button
                      type="primary"
                      style="width: 100%"
                      @click="loginMethods"
                      :disabled="
                        loginData.account == '' || loginData.password == ''
                      "
                      >登录</el-button
                    >
                    <div style="margin-top: 10px">
                      <a href="#" @click.prevent="activeName = 'second'">
                        还没有账号?马上注册
                      </a>
                    </div>
                  </el-form-item>
                </el-form>
              </el-tab-pane>
              <el-tab-pane label="注册" name="second">
                <el-form ref="el-form">
                  <el-form-item label="账号" placeholder="账号长度6-12位">
                    <el-input v-model="registerData.account" />
                  </el-form-item>
                  <el-form-item
                    label="密码"
                    placeholder="密码需长度6-12为含字母数字"
                    style="margin-top: 40px"
                  >
                    <el-input v-model="registerData.password" type="password" />
                  </el-form-item>
                  <el-form-item
                    label="确认密码"
                    placeholder="请再次输入密码"
                    style="margin-top: 40px"
                  >
                    <el-input
                      v-model="registerData.repassword"
                      type="password"
                    />
                  </el-form-item>
                  <el-form-item>
                    <el-button
                      type="primary"
                      style="width: 100%"
                      @click="registerMethods"
                      >注册</el-button
                    >
                  </el-form-item>
                </el-form>
              </el-tab-pane>
            </el-tabs>
          </el-card>
        </div>
      </el-main>
      <el-footer class="footer-wrapped">
        <div class="footer-content">
          <div class="title">
            <span>阿里云专家</span> | <span>阿里云专家</span> |
            <span>阿里云专家</span> |
          </div>
        </div>
      </el-footer>
    </el-container>
    <forget ref="forgetRef" />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import forget from './components/forget-password.vue'
import { login, register } from '@/api/login'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { userInfoStore } from '../../store/userInfo.js'

const router = useRouter()
const store = userInfoStore()
const activeName = ref('first')
interface formData {
  account: number
  password: string
  repassword?: string
}
let loginData: formData = reactive({
  account: null,
  password: '',
})
let registerData: formData = reactive({
  account: null,
  password: '',
  repassword: '',
})
const forgetRef = ref()
const forgetPassword = () => {
  forgetRef.value.open()
}
// 注册
const registerMethods = async () => {
  if (registerData.password == registerData.repassword) {
    const res = await register(registerData)
    if (res.message === '注册账号成功') {
      ElMessage({
        message: '注册成功',
        type: 'success',
      })
      activeName.value = 'first'
      registerData = reactive({
        account: null,
        password: '',
        repassword: '',
      })
    } else {
      ElMessage.error('注册失败，请检查数据是否正确')
    }
  } else {
    ElMessage.error('注册失败，请检查两次密码是否一致')
  }
}

const loginMethods = async () => {
  const res = await login(loginData)
  const { token } = res
  const { id } = res.results
  if (res.message == '登录成功') {
    ElMessage({
      message: '登录成功',
      type: 'success',
    })
    localStorage.setItem('token', token)
    store.getUserInfo(id)
    loginData = reactive({
      account: null,
      password: '',
    })
    router.push('/home')
  } else {
    ElMessage.error('登录失败')
  }
}
</script>

<style lang="scss" scoped>
.header-wrapped {
  .header-content {
    width: 80%;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .welcome {
      font-size: 12px;
      color: #ffffff;
      opacity: 0.8;
    }
  }
}
.common-layout {
  background-image: url(https://myblogphoto-1313051656.cos.ap-guangzhou.myqcloud.com/photos/0w3pdr.jpg);
  height: 100vh;
  width: 100vw;
  background-size: cover;
  background-repeat: no-repeat;
}
.el-main {
  height: 80%;
  display: flex;
  justify-content: center;
  align-items: center;
  .login-warpper {
    width: 50%;
    height: 100%;
    .box-card {
      width: 100%;
      height: 70%;
      opacity: 0.7;
    }
  }
}
.footer-wrapped {
  margin-top: 10px;
  .footer-content {
    width: 80%;
    margin: 0 auto;
    display: flex;
    justify-content: center;
    .title {
      display: flex;
      color: #ffffff;
      opacity: 0.8;
    }
  }
}
.el-form {
  margin-top: 40px;
}
</style>